/*****************************************************************************
 * ct_type.h
 *
 ****************************************************************************/

#ifndef _CT_TYPE_H
#define _CT_TYPE_H

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include <intrins.h>
#include <stdio.h>
// #include <stdint.h>
// #include <stdbool.h>


/*****************************************************************************
 * Enumerations and Structures
 ****************************************************************************/
// Enumerations for boolean values, flag status, error status, etc.
typedef enum { false = 0,
               true } bool;
// typedef enum { FALSE = 0, TRUE  = !FALSE } Bool;
typedef enum { RESET = 0,
               SET   = !RESET } FlagStatus_t,
    ITStatus_t, BitStatus_t;
// typedef enum { DISABLE = 0,
//               ENABLE  = !DISABLE } FuncState_t;
#ifndef DISABLE
#define DISABLE 0
#endif

#ifndef ENABLE
#define ENABLE !DISABLE
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE !FALSE
#endif

typedef enum { ERROR   = 0,
               SUCCESS = !ERROR } ErrorStatus_t;
// typedef enum { LOW  = 0,
//                HIGH = !LOW } PriorityStatus;
/*****************************************************************************
 * Basic Data Type Definitions
 ****************************************************************************/
// ... (基本数据类型定义，如ct_u8, ct_s8等)

#define __CC_ARM
#if defined(__CC_ARM) /* for MDK */
typedef unsigned char ct_u8;
typedef signed char   ct_s8;
typedef unsigned int  ct_u16;
typedef signed int    ct_s16;
typedef unsigned long ct_u32;
typedef signed long   ct_s32;
// typedef unsigned long long ct_u64;
// typedef signed long long   ct_s64;
typedef float  ct_float;
typedef double ct_double;
typedef enum { ct_false = 0,
               ct_true } ct_bool;
typedef void ct_void;
#elif defined(__GNUC__)   /* for Linux */
typedef unsigned char      ct_u8;
typedef char               ct_s8;
typedef unsigned short     ct_u16;
typedef short              ct_s16;
typedef unsigned int       ct_u32;
typedef int                ct_s32;
typedef unsigned long long ct_u64;
typedef long long          ct_s64;
typedef float              ct_float;
typedef double             ct_doule;
typedef enum { ct_false,
               ct_true } ct_bool;
typedef void ct_void;
#elif defined(__ICCARM__) /* for IAR */
typedef unsigned char      ct_u8;
typedef signed char        ct_s8;
typedef unsigned short     ct_u16;
typedef signed short       ct_s16;
typedef unsigned int       ct_u32;
typedef signed int         ct_s32;
typedef unsigned long long ct_u64;
typedef signed long long   ct_s64;
typedef float              ct_float;
typedef double             ct_doule;
typedef void               ct_void;
typedef enum { ct_false,
               ct_true } ct_bool;
#endif

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
#define __I        volatile const /*!< defines 'read only' permissions     */
#define __O        volatile       /*!< defines 'write only' permissions    */
#define __IO       volatile       /*!< defines 'read / write' permissions  */
#define ct_null    (ct_void *)0
#define CT_PRIVATE static
#define CT_CONST   const
#define CT_PUBLIC

#ifndef NULL
#define NULL ((void *)0)    // Define NULL after including <stdio.h>
#endif

#define CT_UNUSED(x) (void)(x)


/*****************************************************************************
 * Type Definitions
 ****************************************************************************/
// Standard integer types (commented out if stdint.h is included)
#ifndef uint8_t
#define uint8_t ct_u8
#endif
#ifndef uint16_t
#define uint16_t ct_u16
#endif
#ifndef uint32_t
#define uint32_t ct_u32
#endif

#ifndef int8_t
#define int8_t ct_s8
#endif
#ifndef int16_t
#define int16_t ct_s16
#endif
#ifndef int32_t
#define int32_t ct_s32
#endif

#ifndef s8
#define s8 ct_s8
#endif
#ifndef s16
#define s16 ct_s16
#endif
#ifndef s32
#define s32 ct_s32
#endif

#ifndef u8
#define u8 ct_u8
#endif
#ifndef u16
#define u16 ct_u16
#endif
#ifndef u32
#define u32 ct_u32
#endif


/*****************************************************************************
 * Compiler-Specific Macros
 ****************************************************************************/
// Compiler-specific keywords for inline assembly, inline functions, etc.
#if defined(__CC_ARM)
#define CT_ASM           __asm
#define CT_INLINE        __inline
#define CT_STATIC_INLINE static __inline
#define CT_USED          __attribute__((used))
#define CT_WEAK          __attribute__((weak))
#define CT_ALIGNED(x)    __attribute__((aligned(x)))
#define CT_PACKED        __attribute__((packed))
#define CT_RESTRICT      __restrict
#elif defined(__ICCARM__)
// ... (IAR Compiler-specific keywords )
#elif defined(__GNUC__)
#define CT_ASM           __asm
#define CT_INLINE        inline
#define CT_STATIC_INLINE static inline
#define CT_USED          __attribute__((used))
#define CT_WEAK          __attribute__((weak))
#define CT_ALIGNED(x)    __attribute__((aligned(x)))
#define CT_PACKED        __attribute__((packed, aligned(1)))
#define CT_RESTRICT      __restrict
#else
#error "Unknown compiler"
#endif


#ifdef __cplusplus
extern "C" {
#endif
/*****************************************************************************
 * Timing Macros
 ****************************************************************************/
#define BEGINED_TICK_MS(TimeInv)        \
    ct_u16 currentTick = ct_tick_get(); \
    if (currentTick - lastTickTime >= TimeInv) {

#define ENDED_TICK_MS           \
    lastTickTime = currentTick; \
    }

#define DELAY_LOOP(count)           \
    do {                            \
        volatile unsigned int i;    \
        for (i = 0; i < count; i++) \
            ;                       \
    } while (0)

/*****************************************************************************
 * Calculate Macros
 ****************************************************************************/
// Calculate the logarithm of the power of 2 macros
#define CT_ABS(x) ((x) < 0 ? -(x) : (x))
#define CT_LOG2(N)       \
    ((N) == 1       ? 0  \
     : (N) == 2     ? 1  \
     : (N) == 4     ? 2  \
     : (N) == 8     ? 3  \
     : (N) == 16    ? 4  \
     : (N) == 32    ? 5  \
     : (N) == 64    ? 6  \
     : (N) == 128   ? 7  \
     : (N) == 256   ? 8  \
     : (N) == 512   ? 9  \
     : (N) == 1024  ? 10 \
     : (N) == 2048  ? 11 \
     : (N) == 4096  ? 12 \
     : (N) == 8192  ? 13 \
     : (N) == 16384 ? 14 \
     : (N) == 32768 ? 15 \
     : (N) == 65536 ? 16 \
                    : 0)



// 计算幂宏 - 计算一个数的n次幂（需要循环宏
#define CT_POW(base, exp)                  \
    ({                                     \
        int _result = 1;                   \
        for (int _i = 0; _i < (exp); _i++) \
            _result *= (base);             \
        _result;                           \
    })
#define CT_SQUARE(x)                   ((x) * (x))
#define CT_DISTANCE_2D(x1, y1, x2, y2) (sqrt(SQUARE((x2) - (x1)) + SQUARE((y2) - (y1))))
#define CT_CIRCLE_AREA(radius)         (3.14159265358979323846F * SQUARE(radius))
#define CT_CUBE(x)                     ((x) * (x) * (x))
#define CT_CUBE_VOLUME(side)           (CUBE(side))
#define CT_SPHERE_VOLUME(radius)       ((4.0F / 3.0F) * 3.14159265358979323846F * CUBE(radius))

// 计算摄氏度到华氏度的转换宏 - 将摄氏度转换为华氏度
#define CT_CELSIUS_TO_FAHRENHEIT(celsius) ((celsius) * 9 / 5 + 32)
// 计算华氏度到摄氏度的转换宏 - 将华氏度转换为摄氏度
#define CT_FAHRENHEIT_TO_CELSIUS(fahrenheit) (((fahrenheit)-32) * 5 / 9)

#define CT_ROTATE_LEFT(var, n)  ((var) << (n)) | ((var) >> (sizeof(var) * 8 - (n)))
#define CT_ROTATE_RIGHT(var, n) ((var) >> (n)) | ((var) << (sizeof(var) * 8 - (n)))
#define CT_BINARY(num)          strtol(#num, NULL, 2)


// 计算二进制中1的个数宏 - 计算一个数的二进制表示中1的个数（汉明重量）：
#define CT_POPCOUNT(x)                                                     \
    ({                                                                     \
        unsigned int _x    = (x);                                          \
        _x                 = _x - ((_x >> 1) & 0x55555555);                \
        _x                 = (_x & 0x33333333) + ((_x >> 2) & 0x33333333); \
        _x                 = (_x + (_x >> 4)) & 0x0F0F0F0F;                \
        _x                 = _x + (_x >> 8);                               \
        \ 
                        _x = _x + (_x >> 16);                              \
        _x & 0x0000003F;                                                   \
    })


/*****************************************************************************
 * Common Utility Macros
 ****************************************************************************/
// Common utility macros for max/min, absolute value, clamping, swapping, etc.
#define CT_MAX(a, b) ((a) > (b) ? (a) : (b))
#define CT_MIN(a, b) ((a) < (b) ? (a) : (b))
// 限制值范围宏 - 将值限制在指定的最小值和最大值之间：
#define CT_CLAMP(x, a, b) (CT_MIN(CT_MAX((x), (a)), (b)))
#define CT_SWAP(a, b)        \
    do {                     \
        typeof(a) tmp = a;   \
        a             = b;   \
        b             = tmp; \
    } while (0)

/*****************************************************************************
 * Bit Manipulation Macros
 ****************************************************************************/
#define CT_BIT_MASK(bit)        (1 << (bit))
#define CT_BIT_SET(var, bit)    ((var) |= (1 << (bit)))
#define CT_BIT_CLEAR(var, bit)  ((var) &= ~(1 << (bit)))
#define CT_BIT_TOGGLE(var, bit) ((var) ^= (1 << (bit)))
#define CT_BIT_CHECK(var, bit)  (!!((var) & (1 << (bit))))


/*****************************************************************************
 * Array and String Operation Macros
 ****************************************************************************/
// Macros for array size, end of array, etc.
#define CT_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#define CT_ARRAY_END(arr)  (&(arr)[CT_ARRAY_SIZE(arr)])
#define CT_ARRAY_SUM(arr, len)             \
    ({                                     \
        int _sum = 0;                      \
        for (int _i = 0; _i < (len); _i++) \
            _sum += (arr)[_i];             \
        _sum;                              \
    })
#define CT_ARRAY_AVG(arr, len) (ARRAY_SUM(arr, len) / (len))

/*****************************************************************************
 * Register Operation Macros
 ****************************************************************************/
// Macros for register read/write operations.
#define CT_CLEAR_REG(reg)        ((reg) = (0x0))
#define CT_READ_REG(reg)         (*(volatile ct_u32 *)(reg))
#define CT_WRITE_REG(reg, value) (*(volatile ct_u32 *)(reg) = (value))
#define CT_MODIFY_REG(reg, CLEARMASK, SETMASK) \
    CT_WRITE_REG((reg), (((CT_READ_REG(reg)) & (~(CLEARMASK))) | (SETMASK)))

/*****************************************************************************
 * Byte and Word Access Macros
 ****************************************************************************/
#define CT_LOBYTE(x)              ((uint8_t)((x) & 0xFF))
#define CT_HIBYTE(x)              ((uint8_t)((x) >> 8))
#define CT_MERGE_BYTES(high, low) (((uint16_t)(high) << 8) | (uint8_t)(low))


#define CT_BHIBYTE(arg) (*((u8 *)&arg + 1))
#define CT_BLOBYTE(arg) (*((u8 *)&arg + 0))

#define CT_BDWORD3BYTE(arg) (*((u8 *)&arg + 3))
#define CT_BDWORD2BYTE(arg) (*((u8 *)&arg + 2))
#define CT_BDWORD1BYTE(arg) (*((u8 *)&arg + 1))
#define CT_BDWORD0BYTE(arg) (*((u8 *)&arg + 0))


/*****************************************************************************
 * Type Casting Macros
 ****************************************************************************/
// Macros for type casting and pointer conversions.
#define CT_CAST(type, ptr)        ((type)(ptr))
#define CT_PTR_TO_UINT(ptr)       ((u16)(ptr))
#define CT_PTR_TO_UINT_PTR(ptr)   ((u16 *)(ptr))
#define CT_PTR_TO_INT(ptr)        ((s16)(ptr))
#define CT_PTR_TO_INT_PTR(ptr)    ((s16 *)(ptr))
#define CT_PTR_TO_CHAR(ptr)       ((s8)(ptr))
#define CT_PTR_TO_CHAR_PTR(ptr)   ((s8 *)(ptr))
#define CT_PTR_TO_SHORT(ptr)      ((s16)(ptr))
#define CT_PTR_TO_SHORT_PTR(ptr)  ((s16 *)(ptr))
#define CT_PTR_TO_LONG(ptr)       ((s32)(ptr))
#define CT_PTR_TO_LONG_PTR(ptr)   ((s32 *)(ptr))
#define CT_PTR_TO_FLOAT(ptr)      ((float)(ptr))
#define CT_PTR_TO_FLOAT_PTR(ptr)  ((float *)(ptr))
#define CT_PTR_TO_DOUBLE(ptr)     ((double)(ptr))
#define CT_PTR_TO_DOUBLE_PTR(ptr) ((double *)(ptr))


/*****************************************************************************
 * Other Macros
 ****************************************************************************/
// Other miscellaneous macros.
#define NOP     _nop_();
#define U8_MAX  ((u8)255)
#define S8_MAX  ((s8)127)
#define S8_MIN  ((s8)-128)
#define U16_MAX ((u16)65535u)
#define S16_MAX ((s16)32767)
#define S16_MIN ((s16)-32768)
#define U32_MAX ((u32)4294967295uL)
#define S32_MAX ((s32)2147483647)
#define S32_MIN ((s32)-2147483648uL)
// ... (其他宏定义)
/****************************************************************************/
#ifdef __cplusplus
}
#endif /* __CT_TYPE_H__ */
/*****************************************************************************
 * End of File
 ****************************************************************************/
#endif
